<!--
 * @Description: Description
 * @Author: wning
 * @Date: 2022-12-09 17:15:33
 * @LastEditors: Andy
 * @LastEditTime: 2023-03-01 16:27:19
-->
<template>
  <x-dialog
    :title="title"
    :show.sync="visible"
    v-if="visible"
    :before-close="handleClose"
    :width="'55%'"
    @onCancel="handleClose"
    @onSubmit="handleSubmit"
  >
    <el-form
      :model="params"
      class="demo-form-inline"
      ref="params"
      :rules="rules"
      label-position="top"
      label-width="auto"
    >
      <el-row :gutter="36">
        <el-col :span="params.apiType + '' === '2' ? 12 : 24" v-if="params.parentId + '' !== '0'">
          <el-form-item :label="$t('auth.interface.parentId')" prop="parentId">
            <x-select-tree
              :result="params.parentId"
              :options="options"
              :defaultProps="defaultProps"
              v-model="params.parentId"
            ></x-select-tree>
          </el-form-item>
        </el-col>
        <el-col :span="params.apiType + '' === '2' ? 12 : 24">
          <el-form-item :label="$t('auth.interface.apiType')" prop="apiType">
            <x-select
              :placeholder="$t('auth.interface.apiTypePlaceholder')"
              v-model="params.apiType"
              :options="dictOptions['upms_api.api_type']"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('auth.interface.apiName')" prop="apiName" v-if="params.apiType + '' === '2'">
            <el-input
              v-model="params.apiName"
              :placeholder="$t('auth.interface.apiNamePlaceholder')"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label="$t('auth.interface.apiUrl')" prop="apiUrl" v-if="params.apiType + '' === '2'">
            <el-input
              v-model="params.apiUrl"
              :placeholder="$t('auth.interface.apiUrlPlaceholder')"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('auth.interface.perms')" prop="perms" v-if="params.apiType + '' === '2'">
            <el-input v-model="params.perms" :placeholder="$t('auth.interface.permsPlaceholder')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="params.apiType + '' !== '2'">
          <el-form-item :label="$t('auth.interface.moduleName')" prop="apiName">
            <el-input
              v-model="params.apiName"
              :placeholder="$t('auth.interface.moduleNamePlaceholder')"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('auth.interface.public')" prop="state">
            <x-radio-group :listData="dictOptions['upms_api.state']" v-model="params.state"></x-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="params.apiType + '' === '2'">
          <el-form-item :label="$t('auth.interface.developer')" prop="developer">
            <el-input
              v-model="params.developer"
              :placeholder="$t('auth.interface.developerPlaceholder')"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('auth.interface.sort')" prop="sort">
            <el-input-number size="small" v-model="params.sort" controls-position="right" :min="1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="params.apiType + '' === '2'">
          <el-form-item :label="$t('system.dict.describe')" prop="remark">
            <el-input
              v-model="params.remark"
              type="textarea"
              :rows="3"
              :placeholder="$t('system.dict.describePlaceholder')"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </x-dialog>
</template>
<script>
  import { cloneDeep, omit, throttle } from '@package/utils'
  import { apiPostInterfaceAdd, apiPostInterfaceFindById, apiPostInterfaceUpdate } from '../api/interface.js'
  import { stateOption, typeOption } from '../config/interface.js'
  import { EnumApiCode } from '../../../../../../globalEnum.js'

  export default {
    props: {
      options: {
        type: Array,
        default: () => []
      },
      dictOptions: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        typeOption,
        stateOption,
        visible: false,
        title: this.$t('auth.interface.addTitle'),
        params: {
          apiName: '',
          apiUrl: '',
          state: '1',
          perms: '',
          remark: '',
          parentId: '',
          apiType: '',
          sort: ''
        },
        rules: {
          apiName: [
            { required: true, message: this.$t('auth.interface.apiNamePlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, trigger: 'blur' }
          ],
          apiUrl: [
            { required: true, message: this.$t('auth.interface.apiUrlPlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, trigger: 'blur' }
          ],
          state: [{ required: true, message: this.$t('auth.interface.statePlaceholder'), trigger: 'blur' }],
          apiType: [{ required: true, message: this.$t('auth.interface.apiTypePlaceholder'), trigger: 'blur' }],
          parentId: [{ required: true, message: this.$t('auth.interface.parentIdPlaceholder'), trigger: 'blur' }],
          remark: [
            { required: true, message: this.$t('auth.interface.remarkPlaceholder'), trigger: 'blur' },
            { min: 1, max: 1000, trigger: 'blur' }
          ]
        },
        // options: [],
        defaultProps: { children: 'children', label: 'apiName', value: 'id' },
        normalizer(node) {
          return {
            id: node.key,
            label: node.name,
            children: node.subOptions
          }
        }
      }
    },
    mounted() {},
    methods: {
      handleOpen(info) {
        this.params.apiType = info.apiType
        if (info?.id) {
          this.title = this.$t('auth.interface.editTitle')
          this.findByInfo(info.id)
        } else {
          // this.params.apiType=info.apiType
          this.params = Object.assign(this.params, cloneDeep(info))
          this.title = this.$t('auth.interface.addTitle')
        }
        this.visible = true
      },
      // 详情
      findByInfo(id) {
        apiPostInterfaceFindById(id).then((res) => {
          if (res.code === EnumApiCode.Success) {
            this.params = Object.assign(this.params, res.data)
            this.params.state += ''
            this.params.apiType += ''
          }
        })
      },
      handleClose() {
        this.$refs.params.resetFields()
        this.params = omit(this.params, 'id')
        this.visible = false
      },
      handleSubmit: throttle(function () {
        this.$refs.params.validate(async (valid) => {
          if (valid) {
            const param = {
              ...this.params
            }
            if (Number(param.apiType) === 1) {
              delete param.apiUrl
              delete param.developer
              delete param.perms
              delete param.remark
            }
            try {
              const { code, msg } = param.id ? await apiPostInterfaceUpdate(param) : await apiPostInterfaceAdd(param)
              if (code === EnumApiCode.Success) {
                if (param.id) {
                  this.$message.success(this.$t('common.editSuccess'))
                } else {
                  this.$message.success(this.$t('common.newSuccess'))
                }
                this.$emit('handleSuccess')
                this.handleClose()
              } else {
                this.$message.error(msg)
              }
            } catch (e) {}
          } else {
            return false
          }
        })
      }, 1000)
    }
  }
</script>
